package com.paranoidvulture.javaEngine.MapLayers;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.InputStream;
import java.util.LinkedList;
import java.util.List;

import javax.imageio.ImageIO;

import com.paranoidvulture.javaEngine.Utilities.Camera2D;

public class EditorLayer implements MouseListener, MouseMotionListener{
	private int tileWidth = 64;
	private int tileHeight = 64;
	private List<Image> tileTypes;
	private TileCell[][] mapArray;
	private Color outLine = Color.blue;
	private Color blank = Color.black;
	private Color highlight = Color.orange;
	private Color selected = Color.red;
	private int mapWidth, mapHeight;
	
	public EditorLayer(String[] tileNames){
		this.tileTypes = new LinkedList<Image>();
		try{
			for (int i = 0; i< tileNames.length; i++){
				 InputStream input = ClassLoader.getSystemResourceAsStream(tileNames[i]);
				 Image temp = ImageIO.read(input);
				 this.tileTypes.add(temp);
			}
		}catch(Exception ex){}
	}
	
	public EditorLayer() {
		
	}

	
	public int getMapWidth() {
		return mapWidth;
	}

	public void setMapWidth(int mapWidth) {
		this.mapWidth = mapWidth;
	}

	public int getMapHeight() {
		return mapHeight;
	}

	public void setMapHeight(int mapHeight) {
		this.mapHeight = mapHeight;
	}

	public int getTileWidth(){
		return tileWidth;
	}
	
	public void setTileWidth(int tileWidth){
		this.tileWidth = tileWidth;
	}
	
	public int getTileHeight(){
		return tileHeight;
	}
	
	public void setTileHeight(int tileHeight){
		this.tileHeight = tileHeight;
	}
	
	public void setMapSize(int width, int height) {
		int w = width * tileWidth;
		int h = height * tileHeight;
		mapArray = new TileCell[w][h];
		this.mapWidth = width;
		this.mapHeight = height;
		
		for (int i = 0; i < mapWidth; i++) {
			for (int j = 0; j < mapHeight; j++) {
				mapArray[i][j].init(j * this.tileWidth, i * this.tileHeight);	
			}
		}
	}
	
	public void draw(Graphics2D g, Camera2D cam) {
		for (int y = 0; y < mapArray.length; y++){
			for (int x = 0; x < mapArray[0].length; x++){
				if (!mapArray[y][x].isBlank()) {
					if(cam.getVirtualRec().intersects(mapArray[y][x].getRec())) {
						g.setColor(mapArray[y][x].getCurColor());
						g.fillRect((int)mapArray[y][x].getPosX()-(int)cam.getVirtualPosX(),
								(int)mapArray[y][x].getPosY()-(int)cam.getPosY(),
								this.tileWidth,
								this.tileHeight);
						g.setColor(outLine);
						g.drawRect((int)mapArray[y][x].getPosX()-(int)cam.getVirtualPosX(),
								(int)mapArray[y][x].getPosY()-(int)cam.getPosY(),
								this.tileWidth,
								this.tileHeight);
					}
				}
			}
		}
	}
	
	public void init(int width, int height) {
		mapArray = new TileCell[width][height];
		this.mapWidth = width;
		this.mapHeight = height;
		for (int i = 0; i < mapWidth; i++) {
			for (int j = 0; j < mapHeight; j++) {
				mapArray[i][j] = new TileCell();
				mapArray[i][j].init(j * this.tileWidth, i * this.tileHeight);
				mapArray[i][j].setCurColor(blank);
			}
		}
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		if (this.mapArray.length > 0) {
			for (int i = 0; i < mapWidth; i++) {
				for (int j = 0; j < mapHeight; j++) {
					if (mapArray[i][j].getRec().contains(e.getX(), e.getY())) {
						if(mapArray[i][j].isSelected()) {
							mapArray[i][j].setSelected(false);
							mapArray[i][j].setCurColor(blank);
						} else {
							mapArray[i][j].setCurColor(selected);
							mapArray[i][j].setSelected(true);
						}
					}
				}
			}
		}
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
	}

	@Override
	public void mouseDragged(MouseEvent arg0) {
	}

	@Override
	public void mouseMoved(MouseEvent e) {
		if (this.mapArray.length > 0) {
			for (int i = 0; i < mapWidth; i++) {
				for (int j = 0; j < mapHeight; j++) {
					if (mapArray[i][j].getRec().contains(e.getX(), e.getY()) && 
							!mapArray[i][j].isSelected()) {
						mapArray[i][j].setCurColor(highlight);
					}
				}
			}
		}
	}
}
